﻿using System;
using System.Drawing;
using System.Web;

namespace sBlog.Net.Upload
{
    /// <summary>
    /// Summary description for MlogUploadVideo
    /// </summary>
    public class MlogUploadVideo : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.Expires = -1;

            if (context.Request["UserId"] != null)
            {
                string userId = context.Request["UserId"];
                string fName = Guid.NewGuid().ToString() + ".mp4";
                try
                {
                    HttpPostedFile postedFile = context.Request.Files["Filedata"];

                    string strFtpAddress = System.Configuration.ConfigurationManager.AppSettings["ftpServerVideo"].ToString() + "/Mlog/UploadVideos";
                    string strFolderUser = clsFunction.GetFolderByDate(strFtpAddress);
                    clsFunction.FtpCreateFolder(strFolderUser, System.Configuration.ConfigurationManager.AppSettings["ftpUserNameVideo"].ToString(), System.Configuration.ConfigurationManager.AppSettings["ftpPasswordVideo"].ToString());

                    string ftpUploadUrl = strFolderUser + "/" + fName;
                    clsFunction.UploadVideoClip(ftpUploadUrl, postedFile);
                    string strDate = DateTime.Now.Year.ToString() + DateTime.Now.ToString("MM");
                    context.Response.StatusCode = 200;
                    context.Response.Write(strDate + fName);
                }
                catch (Exception ex)
                {
                    clsFunction.Log(ex.Message);
                }
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}